package com.intel.factorytest.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class TouchScreen extends Activity {
    private static final String TAG = "TouchScreen";
    private int mWidth = 600;
    private int mHeight = 600;
    private int m_x = 70;
    private int m_y = 70;
    private static final String TEST_ITEM = "test_item";
    private float m_stx =0;
    private float m_sty =0;
    private int[] is_all = new int[4];//={0};
    private int count =0;
    private int whichTest =0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        mWidth  = dm.widthPixels;
        mHeight = dm.heightPixels;

        setContentView(new DrawView(this));
        Log.v(TAG,"mWidth="+mWidth+"mHeight="+mHeight);
    }    

    class DrawView extends View {
        private Paint   mPaint;
        private Paint   mPaint1;
        private Paint   mPaint2;
        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;
        private Rect rect;

        //        private Path mRectpath = new Path();
        /* (non-Javadoc)
         * @see android.view.View#onDraw(android.graphics.Canvas)
         */
    @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
            //            mPaint.setColor(Color.RED);
            //            mPaint.setStrokeWidth(30);
            //            canvas.drawRect(new Rect(0,0,320,480),mPaint);
            mPaint.setStrokeWidth(10);
            mPaint.setColor(Color.WHITE);

            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            canvas.drawPath(mPath, mPaint);

            canvas.drawRect(rect,mPaint1);
            super.onDraw(canvas);
        }

        /**
         * @param context
         */
        public DrawView(Context context) {
            super(context);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(10);
            mPaint1 = new Paint();
            mPaint1.setAntiAlias(true);
            mPaint1.setDither(true);
            mPaint1.setColor(Color.RED);
            mPaint1.setStyle(Paint.Style.STROKE);
            mPaint1.setStrokeJoin(Paint.Join.ROUND);
            mPaint1.setStrokeCap(Paint.Cap.ROUND);
            mPaint1.setStrokeWidth(10);

            mPaint2 = new Paint();
            mPaint2.setAntiAlias(true);
            mPaint2.setDither(true);
            mPaint2.setColor(Color.BLUE);
            mPaint2.setStyle(Paint.Style.STROKE);
            mPaint2.setStrokeJoin(Paint.Join.ROUND);
            mPaint2.setStrokeCap(Paint.Cap.ROUND);
            mPaint2.setStrokeWidth(10);
            mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath  = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            rect = new Rect(m_x,m_y,mWidth-m_x,mHeight-m_y);
        }

    @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    m_stx = x;
                    m_sty = y;
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }

        private void touch_start(float x, float y) {
            //  mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) {
            if (whichTest == 0) {
                isPass(x,y);
            }
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }

        private void touch_up() {
            mPath.lineTo(mX, mY);
            //  mCanvas.drawPath(mPath, mPaint);
            //  mPath.reset();
            float dx = Math.abs(m_stx - mX);
            float dy = Math.abs(m_sty - mY);
            int all = is_all[0]+is_all[1]+is_all[2]+is_all[3];
            Log.e(TAG,"touch_up ***  all ="+all+"dx =" +dx+"dy="+dy);
            if (whichTest == 0) {
                if (all == 4 && dx <= (TOUCH_TOLERANCE+25) && dy <= (TOUCH_TOLERANCE+25)) {
                    mPath.reset();
                    // mPaint1.setAlpha(0);
                    rect.setEmpty();
                    mCanvas.drawLine(mWidth-m_x,0,0,mHeight-m_y,mPaint2);
                    mCanvas.drawLine(mWidth,m_y,m_x,mHeight,mPaint2);
                    mCanvas.drawLine(0,m_y,mWidth-m_x,mHeight,mPaint2);
                    mCanvas.drawLine(m_x,0,mWidth,mHeight-m_y,mPaint2);
                    whichTest = 1;
                //    Log.e(TAG,"### up pass");
                }else {
                    Toast.makeText(TouchScreen.this, "Test failed !!!!!", Toast.LENGTH_SHORT).show();
                //    Log.e(TAG,"**** up failed ");
                }
            }
        }

        public void isPass (float x,float y) {
            boolean bl = rect.contains((int)x,(int)y);
            if (bl && count ==0) {
                //Log.e(TAG,"fail!!!!");
                Toast.makeText(TouchScreen.this, "Test failed !!!!!", Toast.LENGTH_SHORT).show();
                count++;
            }
            float dx = mX-x;
            float dy = mY-y;
            if (dy<0) {
                is_all[0]=1;//down
            } else if (dx<0) {
                is_all[1]=1;//right
            }else if (dy>0) {
                is_all[2]=1;
            }else if (dx>0) { 
                is_all[3]=1; 
            }
            /*
               if (x>m_x && x<mWidth-m_x) {
               if ((y>0 &&y<m_y)) {
               is_all[3]=1;
               }else if (y>mHeight-m_y && y< mHeight) {
               is_all[1]=1;
               }
               }
               if (x>0 && x<m_x) {
               if (y>0 &&y<mHeight) {
               is_all[0]=1;
               }
               }
               if (x>mWidth-m_x && x<mWidth) {
               if (y>0 &&y<mHeight) {
               is_all[2]=1;
               }
               }
               */
        }
    }

    private void setTestResult () {
        Intent intent = new Intent();
        Intent mIntent = getIntent();
        int pos = mIntent.getIntExtra(TEST_ITEM,-1);
        intent.putExtra(TEST_ITEM,pos);
        //        Log.e(TAG,"pos ="+pos);
        setResult(0, intent);
    }
    @Override
    public void finish() {
        setTestResult (); 
        super.finish();
    }
}
